<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import {CaseApi} from '@/api/case'
import { useAuthStore } from '@/stores/authStore'


// 表单数据
const formData = ref({
  userId: useAuthStore().currentUser.id, // 从本地存储获取用户ID
  type: '',
  description: '',
  files: [] // 改为数组支持多文件
})

// 当前步骤（1-3）
const currentStep = ref(1)

// 案件类型选项
const caseTypes = [
  { value: 'CIVIL', label: '民事案件' },
  { value: 'CRIMINAL', label: '刑事案件' },
  { value: 'ADMINISTRATIVE', label: '行政案件' }
]

// 文件上传处理
const handleFileUpload = (event) => {
  const uploadedFiles = Array.from(event.target.files)
  formData.value.files.push(...uploadedFiles) // 添加多个文件
}

// 移除文件
const removeFile = (index) => {
  formData.value.files.splice(index, 1)
  // 清除input值，允许重新选择相同文件
  const fileInput = document.getElementById('file-upload');
  if (fileInput) fileInput.value = '';
}

// 跳转步骤
const goToStep = (step) => {
  currentStep.value = step
}

// 提交表单
const submitForm = async () => {
  // 表单验证
  if (!formData.value.userId) {
    ElMessage.error('请先登录')
    return
  }
  
  if (!formData.value.type) {
    ElMessage.error('请选择案件类型')
    return
  }
  
  if (!formData.value.description) {
    ElMessage.error('请描述案情')
    return
  }
  
  if (formData.value.files.length === 0) {
    ElMessage.error('请上传相关材料')
    return
  }
  
    // 创建FormData对象
    const formDataToSend = new FormData()
    formDataToSend.append('userId', formData.value.userId)
    formDataToSend.append('type', formData.value.type)
    formDataToSend.append('description', formData.value.description)
    // 添加多个文件
    formData.value.files.forEach((file, index) => {
      formDataToSend.append('files', file)
    })
    
    // 使用case.js中的API方法发送请求
    await CaseApi.applyCase(formDataToSend)
    
    ElMessage.success('案件申请成功！')
    resetForm()
  
}

// 重置表单
const resetForm = () => {
  formData.value = {
    userId: useAuthStore().currentUser.id,
    type: '',
    description: '',
    files: []
  }
  currentStep.value = 1
}
</script>

<template>
  <div class="legal-aid-application">
    <h2>法律援助案件申请</h2>
    
    <!-- 可点击的步骤指示器 -->
    <div class="step-indicator">
      <div 
        v-for="step in 3" 
        :key="step"
        :class="['step', { 
          'active': currentStep === step,
          'completed': formData.type && step === 1 || 
                      formData.description && step === 2 || 
                      formData.files.length > 0 && step === 3
        }]"
        @click="goToStep(step)"
      >
        <span class="step-number">{{ step }}</span>
        <span class="step-title">
          {{ step === 1 ? '选择类型' : step === 2 ? '描述案情' : '上传材料' }}
        </span>
      </div>
    </div>
    
    <!-- 表单内容 -->
    <div class="form-content">
      <!-- 第一步：选择案件类型 -->
      <div v-show="currentStep === 1" class="step-content">
        <h3>请选择案件类型</h3>
        <div class="case-type-options">
          <div 
            v-for="type in caseTypes" 
            :key="type.value"
            :class="['case-type', { 'selected': formData.type === type.value }]"
            @click="formData.type = type.value"
          >
            <div class="case-icon">
              <span v-if="type.value === 'CIVIL'">⚖️</span>
              <span v-else-if="type.value === 'CRIMINAL'">🚨</span>
              <span v-else>🏛️</span>
            </div>
            <span class="case-label">{{ type.label }}</span>
          </div>
        </div>
      </div>
      
      <!-- 第二步：描述案情 -->
      <div v-show="currentStep === 2" class="step-content">
        <h3>请描述您的案情</h3>
        <textarea 
          v-model="formData.description" 
          placeholder="请详细描述案件情况（建议包含时间、地点、涉及人员等）"
        ></textarea>
        <div class="char-count">{{ formData.description.length }}/500</div>
      </div>
      
      <!-- 第三步：上传材料 -->
      <div v-show="currentStep === 3" class="step-content">
        <h3>上传相关材料</h3>
        <div class="upload-area">
          <label for="file-upload" class="upload-btn">
            <span>+</span>
            <p>点击或拖拽文件到此处</p>
            <input 
              id="file-upload" 
              style="display: none;"
              type="file" 
              multiple 
              @change="handleFileUpload"
            >
          </label>
          <div v-if="formData.files.length > 0" class="file-list">
            <div v-for="(file, index) in formData.files" :key="index" class="file-item">
              <span class="file-name">{{ file.name }}</span>
              <span class="file-size">({{ (file.size / 1024).toFixed(2) }} KB)</span>
              <button @click="removeFile(index)" class="remove-btn">×</button>
            </div>
          </div>
        <div v-else class="no-files">尚未上传任何文件</div>
        </div>
      </div>
    </div>
    
    <!-- 导航按钮 -->
    <div class="navigation-buttons">
      <button 
        v-if="currentStep > 1" 
        @click="goToStep(currentStep - 1)" 
        class="btn prev-btn"
      >
        上一步
      </button>
      <button 
        v-if="currentStep < 3" 
        @click="goToStep(currentStep + 1)" 
        class="btn next-btn"
      >
        下一步
      </button>
      <button 
        v-if="currentStep === 3" 
        @click="submitForm" 
        class="btn submit-btn"
      >
        提交申请
      </button>
    </div>
  </div>
</template>

<style scoped>
/* 主容器 - 紧凑布局 */
.legal-aid-application {
  width: 100%;
  max-width: none;
  padding: 25px 30px;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 3px 15px rgba(0, 0, 0, 0.1);
  box-sizing: border-box;
}

h2 {
  text-align: center;
  color: #2c3e50;
  margin-bottom: 30px;
  font-size: 1.8em;
}

/* 步骤指示器 - 可点击 */
.step-indicator {
  display: flex;
  justify-content: space-between;
  margin-bottom: 30px;
  position: relative;
}

.step-indicator::before {
  content: '';
  position: absolute;
  top: 15px;
  left: 0;
  right: 0;
  height: 2px;
  background-color: #e0e0e0;
  z-index: 1;
}

.step {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  z-index: 2;
  cursor: pointer;
  opacity: 0.6;
  transition: all 0.3s;
  flex: 1;
}

.step:hover {
  opacity: 0.9;
}

.step.active {
  opacity: 1;
}

.step.completed .step-number {
  background-color: #2ecc71;
  color: white;
}

.step.completed .step-title {
  color: #2ecc71;
}

.step-number {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background-color: #e0e0e0;
  color: #999;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  margin-bottom: 5px;
  transition: all 0.3s;
}

.step.active .step-number {
  background-color: #3498db;
  color: white;
  transform: scale(1.1);
}

.step-title {
  color: #999;
  font-size: 0.9em;
  text-align: center;
}

/* 步骤内容区 */
.step-content {
  padding: 25px;
  background-color: #f9f9f9;
  border-radius: 8px;
  margin-bottom: 25px;
}

h3 {
  color: #333;
  margin-top: 0;
  margin-bottom: 15px;
}

/* 案件类型选项 */
.case-type-options {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 20px;
  margin-top: 25px;
}

.case-type {
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 8px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
}

.case-type:hover {
  border-color: #3498db;
  background-color: #f5fbff;
}

.case-type.selected {
  border-color: #3498db;
  background-color: #e1f0ff;
}

.case-icon {
  font-size: 28px;
  margin-bottom: 10px;
}

.case-label {
  font-weight: 500;
  font-size: 1em;
}

/* 文本输入区 */
textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  resize: vertical;
  min-height: 150px;
  font-family: inherit;
  font-size: 1em;
  margin-top: 15px;
  line-height: 1.5;
}

textarea:focus {
  outline: none;
  border-color: #3498db;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.2);
}

.char-count {
  text-align: right;
  font-size: 0.8em;
  color: #777;
  margin-top: 5px;
}

/* 文件上传区 */
.upload-area {
  border: 2px dashed #ddd;
  border-radius: 8px;
  padding: 25px;
  text-align: center;
  margin-top: 25px;
  transition: all 0.3s;
}

.upload-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #777;
}

.upload-btn span {
  font-size: 36px;
  line-height: 1;
  margin-bottom: 10px;
}

.upload-btn p {
  margin: 5px 0 0;
}

/* 文件列表 */
.file-list {
  margin-top: 25px;
  text-align: left;
}

.file-item {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  background-color: #f5f5f5;
  border-radius: 6px;
  margin-bottom: 8px;
  font-size: 0.95em;
}

.file-name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-size {
  color: #777;
  margin: 0 10px;
  font-size: 0.8em;
}

.remove-btn {
  background: none;
  border: none;
  color: #e74c3c;
  font-size: 1.2em;
  cursor: pointer;
  padding: 0 5px;
}

.no-files {
  color: #777;
  font-style: italic;
  margin-top: 15px;
}

/* 导航按钮 */
.navigation-buttons {
  display: flex;
  justify-content: flex-end;
  margin-top: 25px;
  gap: 15px;
}

.btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
  font-size: 1em;
}

.prev-btn {
  background-color: #f1f1f1;
  color: #333;
}

.prev-btn:hover {
  background-color: #e0e0e0;
}

.next-btn, .submit-btn {
  background-color: #3498db;
  color: white;
}

.next-btn:hover, .submit-btn:hover {
  background-color: #2980b9;
}

.submit-btn {
  background-color: #2ecc71;
}

.submit-btn:hover {
  background-color: #27ae60;
}

/* 响应式设计 */
@media (max-width: 900px) {
  .legal-aid-application {
    padding: 15px;
  }
  
  .case-type-options {
    grid-template-columns: 1fr;
  }
  
  .step-title {
    display: none;
  }
  
  .step-content {
    padding: 20px;
    margin-bottom: 20px;
  }
  
  .navigation-buttons {
    margin-top: 20px;
  }
}

@media (max-width: 480px) {
  .step-number {
    width: 25px;
    height: 25px;
    font-size: 0.9em;
  }
  
  .step-indicator::before {
    top: 12px;
  }
  
  .btn {
    padding: 8px 15px;
    font-size: 0.9em;
  }
}
</style>